package cc.cix.gt.org.controller;

import cc.cix.gt.common.annotation.Limit;
import cc.cix.gt.common.exception.SystemException;
import cc.cix.gt.common.util.ValidateCodeUtil;
import cc.cix.gt.org.dto.request.LoginReq;
import cc.cix.gt.org.dto.response.ImageCodeResp;
import cc.cix.gt.org.dto.response.LoginResp;
import cc.cix.gt.org.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author cix
 */
@Api(tags = "登录管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/login")
public class LoginController {

    private final IUserService userService;

    @ApiOperation("图形验证码")
    @GetMapping("/captcha")
    public ImageCodeResp captcha(HttpServletRequest request) throws SystemException {
        return ValidateCodeUtil.create(request);
    }

    @Limit(key = "login", period = 60, count = 10, name = "登录接口", prefix = "limit")
    @ApiOperation("用户登录")
    @PostMapping
    public LoginResp login(HttpServletRequest request, @Valid @RequestBody LoginReq dto)
            throws SystemException {
        return userService.login(request, dto);
    }

}
